// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    var self=this;
    self.autoUpdate().then(res=>{ 
        // console.log(11); 
         console.log(res);
       //  that.wxUpdate(); //that.wxLogIn();  
      })
    this.globalData.updateStatus=1;
    const capsule = wx.getMenuButtonBoundingClientRect();
    
    console.log(capsule);
  
    var res = wx.getWindowInfo();
    console.log(res);
        if (res.screenHeight - res.windowHeight - res.statusBarHeight - 32 > 72) {
          self.globalData.isFullSucreen = true
        }
        let navHeight = res.statusBarHeight
        if(res.platform == 'ios' || res.platform=='devtools'){
          self.globalData.platform = 'ios';
          
        }else if(res.platform == 'android'){
          self.globalData.platform = 'android';
        }
        self.globalData.pixelRatio = res.pixelRatio;
        self.globalData.navHeight = capsule.height;
        self.globalData.navTitleTop = capsule.top;
        self.globalData.navWidth = capsule.width;
        self.globalData.navRight =capsule.right;
        self.globalData.statusBarHeight=res.statusBarHeight;
        self.globalData.windowHeight=res.windowHeight;
        self.globalData.screenHeight=res.screenHeight;
        self.globalData.windowWidth=res.windowWidth-2;
        self.globalData.screenWidth=res.screenWidth-2;
       self.globalData.safeHeight = res.safeArea.height;

  },
  autoUpdate:function(){
    var that = this;
    return new Promise(function(resolve, reject) {
      if(wx.canIUse('getUpdateManager')){//获取小程序更新机制兼容 
       const updateManager=wx.getUpdateManager();
      //判断改小程序是否有新版本
      updateManager.onCheckForUpdate(function(res){
        //如果有新版本，更新
        if(res.hasUpdate){
          //准备更新
          updateManager.onUpdateReady(function(){
            wx.showModal({
              title:'更新提示',
              content:'新版本已经准备好，是否重启应用？',
            success:function(res){
              if(res.confirm){
                  updateManager.applyUpdate();
                   resolve('update')          
              }else if(res.cancel){
                console.log("do not update");
                resolve('do not update');                
              }
            }
            })
          })
          updateManager.onUpdateFailed(function(){
            wx.showModal({
              title:'更新失败',
              content:'新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟'
            })
          })          
        }else{
         //console.log('=====>no update');
         resolve('=====>no update');
        }
      })
      }
    })
  },
  goto:function(url='#',index='#'){
    console.log(url);
    
    if(url!="#"){

    if(index=='index'){
     wx.reLaunch({
       url: url,
     })
    }else{
        wx.navigateTo({
          url: url,
        })
    }
    }
   
    }, 
    showModals:function(msg,url,index){
        let that = this;
        wx.showModal({
        // cancelColor: 'cancelColor',
        title:'系统提示',
        content:msg,
        //showCancel:false,
        success:function(e){
            if (e.confirm) {
            that.goto(url,index);
            }
        }
        

        })

    },
    showModal:function(msg,url,index){
        let that = this;
        wx.showModal({
        // cancelColor: 'cancelColor',
        title:'系统提示',
        content:msg,
        //showCancel:false,
        success:function(e){
            if (e.confirm) {
            that.goto(url,index);
            }
        }
        

        })

    },
  request(url,data,type='get'){
    let that = this;
    return new Promise(function(resolve, reject) {
       console.log(data)  
        wx.request({
          url: that.globalData.domain+url,
          data:data,
          method:type,
          success:res=>{
              if(res.statusCode == 200){
                let d = '2024';
                resolve(res.data);
              }else{
                   that.showModals('网络错误！');
              }
          }
        })
        //resolve();
    })
    },
  globalData: {
    open: null,
    domain:'https://www.lypb.cn/',
    appUrl:'https://www.lypb.cn/wechat/',
  }
})
